//
//  BanJiController.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/13.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BanJiController.h"
#import "CJYearMonthSelectedView.h"
#import "SYRequestAPI+Teach.h"
#import "BanJiListModel.h"
#define CellSP 29*fw
#define CellW 50*fw

@interface BanJiController ()<BanJiTopViewSelectedDelegate>
{
    BOOL Type;//type判断整个页面是属于小页面还是大页面
    int pageIndexForWei;//未结业的pageindex
    int pageSizeForWei;//未结业的pageSoze
    int pageIndexForYi;//已结业的pageindex
    int pageSizeForYi;//w已结业的pageSize
    NSString *date;//时间选择
    BOOL jieYeType;//结业类型NO：未结业  YES：已结业
}
@property (nonatomic, strong) NSMutableArray *weiJieYeArray;//未结业数组
@property (nonatomic, strong) NSMutableArray *yiJieYeArray;//已结业数组
@end
@implementation BanJiController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)initSubView{
    Type = YES;
    jieYeType = NO;
    pageIndexForWei = 1;
    pageSizeForWei = 10;
    pageIndexForYi = 1;
    pageSizeForYi = 10;
    _weiJieYeArray = [NSMutableArray new];
    _yiJieYeArray = [NSMutableArray new];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[BanJiCell class] forCellWithReuseIdentifier:@"BanJiCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    // Do any additional setup after loading the view.
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@44);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0).offset(-20);
    }];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.banJiTopView];
    [self.banJiTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"ChangeCellSpaNotice" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showView) name:@"BanJiShowView" object:nil];
    
    [self.collectionView headerRefreshBlock:^{
      //  [self.collectionView.mj_header beginRefreshing]; 这代码写的我也是考狗了---维护者留言
        if (self->jieYeType == NO) {
            [self->_weiJieYeArray removeAllObjects];
            self->pageIndexForWei = 1;
            [self getBanJiLieBiaoRequest];
        }else{
            [self->_yiJieYeArray removeAllObjects];
            self->pageIndexForYi = 1;
            [self getBanJiLieBiaoRequest];
        }
        
    }];
    [self.collectionView fooderRefreshBlock:^{
     //   [self.collectionView.mj_footer beginRefreshing];
        if (self->jieYeType == NO) {//未结业
//            self->pageIndexForWei = (int)self->_weiJieYeArray.count/10+1;
        } else {
//            self->pageIndexForYi = (int)self->_yiJieYeArray.count/10+1;
        }
        [self getBanJiLieBiaoRequest];
    }];
    
    [self.collectionView.mj_header beginRefreshing];
    [self getBanJiLieBiaoRequest];
}
- (void)yiBuRefresh{
    NSOperationQueue *operationQueue=[[NSOperationQueue alloc]init];
    operationQueue.maxConcurrentOperationCount=2;//设置最大并发线程数
    [operationQueue addOperationWithBlock:^{
       
    }];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        
    }];
}
- (void)showView{
    self.collectionView.hidden = NO;
    self.banJiTopView.hidden = NO;
}
- (void)changeView:(NSNotification *)info{
    BOOL type = [[info.userInfo objectForKey:@"Type"] intValue];
    if (type == YES) {
        Type = NO;
    }else{
        Type = YES;
    }
    [self.collectionView reloadData];
}
- (BanJiTopView *)banJiTopView{
    if (!_banJiTopView) {
        BanJiTopView *topView = [[BanJiTopView alloc]init];
        topView.frame = CGRectMake(0, 0, 875*fw, 44);
        topView.delegate = self;
        _banJiTopView = topView;
    }
    return _banJiTopView;
}
- (void)leftButtonClick{
    //未结业
    jieYeType = NO;
    [self.collectionView reloadData];
}
- (void)rightButtonClick{
    //已结业
    jieYeType = YES;
    if (_yiJieYeArray.count == 0) {
        //[self.collectionView.mj_header beginRefreshing];
        [self getBanJiLieBiaoRequest];
    }else{
        [self.collectionView reloadData];
    }
}

- (void)timeButtonClick{
    [CJYearMonthSelectedView showDatePickerWithTitle:@"选择月份" minDateStr:@"2000-01" resultBlock:^(NSString *selectValue) {
        //选择完成后的操作
      //  NSLog(@"selected month is %@", selectValue);
        if ([selectValue isEqualToString:@"全部"]) {
            selectValue = @"";
        } else {
            self->_banJiTopView.timeButton.bottomLabel.text = [self getTimeCurrent:selectValue];
        }
        self->date = selectValue;
        if (self->jieYeType == YES) {
            self->pageIndexForYi = 1;
        }else{
            self->pageIndexForWei = 1;
        }
        [self->_yiJieYeArray removeAllObjects];
        [self->_weiJieYeArray removeAllObjects];
        [self getBanJiLieBiaoRequest];
    
        
    }];
}
-(NSString *)getTimeCurrent:(NSString *)sp{
    NSArray *array = [sp componentsSeparatedByString:@"-"];
    NSString *dateString = [NSString stringWithFormat:@"%@年%@月",array[0],array[1]];
    return dateString;
}
#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (jieYeType == NO) {
        return _weiJieYeArray.count;
    }else{
        return _yiJieYeArray.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BanJiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BanJiCell" forIndexPath:indexPath];
    if (jieYeType == NO) {
        //未结业
        BanJiListModel *model = _weiJieYeArray[indexPath.row];
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",model.className];
        [cell setCourseNameName:model.courseName];
        [cell setMentorName:model.teachers];
        [cell setDangQianJingDuName:[NSString stringWithFormat:@"当前进度：%@",model.lessonTime]];
        [cell setShangKeShiJianName:model.openDate];
        [cell setNianJiLabelName:model.type];
        [cell setKeMuLableName:@"科目"];
        [cell setZhaoShengRenShuLabelName:[NSString stringWithFormat:@"%@/%@",model.students,model.maxStudents]];
        [cell setJiaoShiLabelName:[NSString stringWithFormat:@"%@-%@",model.campusName,model.classroomName]];
    }else{
        //已结业
        BanJiListModel *model;
        @try {
            model = _yiJieYeArray[indexPath.row];
        } @catch (NSException *exception) {
            
        } @finally {
            
        }
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",model.className];
        [cell setCourseNameName:model.courseName];
        [cell setMentorName:model.teachers];
        [cell setDangQianJingDuName:[NSString stringWithFormat:@"全部课时：%@",model.lessonTime]];
        [cell setShangKeShiJianName:model.openDate];
        [cell setNianJiLabelName:model.type];
        [cell setKeMuLableName:@"科目"];
        [cell setZhaoShengRenShuLabelName:[NSString stringWithFormat:@"%@/%@",model.students,model.maxStudents]];
        [cell setJiaoShiLabelName:[NSString stringWithFormat:@"%@-%@",model.campusName,model.classroomName]];
    }
    return cell;
}
#pragma mark == collectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    //top, left, bottom, right
    if (Type == YES) {
        return CGSizeMake(318*fw, 223*fh);
    }else{
        return CGSizeMake(350*fw, 223*fh);
    }
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //top, left, bottom, right
    if (Type == YES) {
        return UIEdgeInsetsMake(CellSP, CellSP, CellSP,CellSP);
    }else{
        return UIEdgeInsetsMake(CellSP, CellW, CellSP,CellW);
    }
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return CellSP;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return CellSP;
}

//是否允许移动Item
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0)
{
    return YES;
}
//移动Item时触发的方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);
{
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //作业
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    if (jieYeType == NO) {
        //未结业
        BanJiListModel *model  = _weiJieYeArray[indexPath.row];
        _listView = [[BanJiViewList  alloc]init];
        _listView.classID = model.classId;
        _listView.courseName = model.courseName;
        _listView.renKeLaoShiName = model.teachers;
        _listView.students = model.students;
        [_listView reloadData];
        _listView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_listView];
        [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.top.equalTo(@0);
            make.height.equalTo(@0).offset(APP_HEIGHT);
        }];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }else{
        //已结业
        BanJiListModel *model  = _yiJieYeArray[indexPath.row];
        _listView = [[BanJiViewList  alloc]init];
        _listView.classID = model.classId;
        _listView.courseName = model.courseName;
        _listView.renKeLaoShiName = model.teachers;
        _listView.students = model.students;
        [_listView reloadData];
        _listView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_listView];
        [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.top.equalTo(@0);
            make.height.equalTo(@0).offset(APP_HEIGHT);
        }];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }
//    [PublickFunctionObject changeMainView:@"TeachChangeNotice" AndType:Type];
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    
}
- (void)getDataForView:(NSString *)name{
//    tableName = name;
    [self.collectionView reloadData];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"BanJiShowView" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"BanJiAddNextViewNotice" object:nil];
}
#pragma mark -教学-班级-班级列表
- (void)getBanJiLieBiaoRequest{
    if (date.length == 0) {
        date = @"";
    }
    if (jieYeType == NO) {
        //未结业
        NSDictionary *parama = @{@"isFinished":@"0",
                                 @"date":date,
                                 @"pageIndex":[NSString stringWithFormat:@"%d",pageIndexForWei],
                                 @"pageSize":[NSString stringWithFormat:@"%d",pageSizeForWei]};
        [MBProgressHUD mj_hideHUDForView:self.collectionView];
//        [MBProgressHUD mj_showMessage:nil toView:self.collectionView];
        [SYRequestAPI sy_getClassServiceClassList:parama success:^(id  _Nonnull responseObject) {
           // NSLog(@"得到-教学-班级-班级列表-未结业 %@",responseObject);
            NSArray *array = [responseObject objectForKey:@"data"];
            for (NSDictionary *dic in array) {
                BanJiListModel *model = [BanJiListModel yy_modelWithJSON:dic];
                [self->_weiJieYeArray addObject:model];
            }

            if (array.count>0) {
                self->pageIndexForWei++;
            }

            [self.collectionView reloadData];
//            [MBProgressHUD mj_hideHUDForView:self.collectionView];
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
        } fail:^(NSError *error) {
            [XSInfoView showInfo:error.message onView:self];
//            [MBProgressHUD mj_hideHUDForView:self.collectionView];
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
        }];
    }else{
        //结业
        NSDictionary *parama = @{@"isFinished":@"1",
                                 @"date":date,
                                 @"pageIndex":[NSString stringWithFormat:@"%d",pageIndexForYi],
                                 @"pageSize":[NSString stringWithFormat:@"%d",pageSizeForYi]};
        [MBProgressHUD mj_hideHUDForView:self.collectionView];
//        [MBProgressHUD mj_showMessage:nil toView:self.collectionView];
        [SYRequestAPI sy_getClassServiceClassList:parama success:^(id  _Nonnull responseObject) {
          //  NSLog(@"得到-教学-班级-班级列表-已结业 %@",responseObject);
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
            NSArray *array = [responseObject objectForKey:@"data"];
            if (array.count>0) {
                self->pageIndexForYi++;
            }
            for (NSDictionary *dic in array) {
                BanJiListModel *model = [BanJiListModel yy_modelWithJSON:dic];
                [self->_yiJieYeArray addObject:model];
            }
            [self.collectionView reloadData];
//            [MBProgressHUD mj_hideHUDForView:self.collectionView];
            
        } fail:^(NSError *error) {
            [XSInfoView showInfo:error.message onView:self];
//            [MBProgressHUD mj_hideHUDForView:self.collectionView];
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
        }];
    }

}
//ios函数
void GetHanSHu(NSString *hanchu){
    
}
@end
